<?php
//sleep(5);
     
    include_once('connection/connection.php');
    
	if(isset($_REQUEST['add_circle'])) {
	mysql_query("INSERT INTO tbl_lookup_circle(circle_id,circle_name,zone_id,zone_name,status) VALUES('','$_REQUEST[add_circle]','$_REQUEST[zone_id]',(SELECT zone_name FROM tbl_lookup_zone WHERE zone_id=$_REQUEST[zone_id]),'1') ");
	$last_id = mysql_insert_id();
	$query = mysql_query("SELECT * FROM tbl_lookup_circle WHERE circle_id='$last_id'");
		$circle_detail = mysql_fetch_assoc($query);
		echo "<tr onMouseOver=\" this.style.backgroundColor='#E5E5E5'; \" onMouseOut= \"this.style.backgroundColor='#FFFFFF'; \" id= 'row_id_".$circle_detail['circle_id']."'><td height='27' width='100'  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$circle_detail['zone_name']."</font></td>";
		echo "<td align='center' width='400' id='circle_id_".$circle_detail['circle_id']."' ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$circle_detail['circle_name']."</font></td>";
		echo "<td align='center'><a href='Javascript:void(0);' onClick= \"edit_zone('".$circle_detail['circle_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Edit</font></a>&nbsp;&nbsp;&nbsp;&nbsp;";
		echo "<span id='state_id_".$circle_detail['circle_id']."' ><a href='Javascript:void(0);' onClick= \" status_handler(0,'".$circle_detail['circle_id']."'); \"  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Disable</font></a></span>";

	unset($last_id);
	unset($query);
	unset($circle_detail);
	
	}
	
	if(isset($_REQUEST['status'])) {

			if(mysql_query("UPDATE tbl_lookup_circle SET status = '$_REQUEST[status]' WHERE circle_id='$_REQUEST[id]' ")) {
					if($_REQUEST['status']==1) echo "<a href='Javascript:void(0);' onClick=\"status_handler(0,'".$_REQUEST['id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Disable</font></a>";
					else echo "<a href='Javascript:void(0);' onClick=\"status_handler(1,'".$_REQUEST['id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Enable</font></a>";
			}
			else{
			echo mysql_error();
			}
	}
	
	if(isset($_REQUEST['edit_id'])){

	$query = mysql_query("SELECT circle_id,circle_name,zone_id,zone_name FROM tbl_lookup_circle WHERE circle_id = '$_REQUEST[edit_id]' ");
	$detail = mysql_fetch_assoc($query);
	//echo "<td>sfsdfs</td>";

	
	echo "<td height='27' ><select id='zone_id' >";
	$query = mysql_query("SELECT * FROM tbl_lookup_zone WHERE status = 1 ORDER BY zone_id desc");
		while($zone = mysql_fetch_assoc($query)){
			if($zone['zone_id'] == $detail['zone_id']) {
			echo "<option value='".$zone['zone_id']."' selected>".$zone['zone_name']."</option>";
			}
			else{
			echo "<option value='".$zone['zone_id']."'>".$zone['zone_name']."</option>";
			}
		}
	echo "</select></td>"; 
	echo "<td width='400' align='center' ><input type='text' id='circle_name' value='".$detail['circle_name']."'  /></td>";
	
	echo "<td align='center'><a href='Javascript:void(0);' onClick= \" save_changes('".$_REQUEST['edit_id']."','circle_name','zone_id');  \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Save</font></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='Javascript:void(0);' onClick= \" default_row('".$_REQUEST['edit_id']."');  \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Cancel</font></a></td><td width='100'>&nbsp;</td>";

		unset($query);
		unset($detail);
		
	
	}
	
	if(isset($_REQUEST['new_circle'])) {
	//print_r($_REQUEST);
		
		mysql_query("UPDATE tbl_lookup_circle SET circle_name = '$_REQUEST[new_circle]' , zone_id = '$_REQUEST[new_zone]' , zone_name = (SELECT zone_name FROM tbl_lookup_zone WHERE zone_id= $_REQUEST[new_zone]) WHERE circle_id = '$_REQUEST[id]'");
		mysql_query("UPDATE tbl_lookup_division SET circle_id= '$_REQUEST[id]' , circle_name = (SELECT circle_name FROM tbl_lookup_circle WHERE circle_id= $_REQUEST[id]) , zone_id = '$_REQUEST[new_zone]' , zone_name = (SELECT zone_name FROM tbl_lookup_zone WHERE zone_id= $_REQUEST[new_zone]) WHERE circle_id = '$_REQUEST[id]' ");
		mysql_query("UPDATE tbl_lookup_sub_division SET circle_id= '$_REQUEST[id]' , circle_name = (SELECT circle_name FROM tbl_lookup_circle WHERE circle_id= $_REQUEST[id]) , zone_id = '$_REQUEST[new_zone]' , zone_name = (SELECT zone_name FROM tbl_lookup_zone WHERE zone_id= $_REQUEST[new_zone]) WHERE circle_id = '$_REQUEST[id]' ");
		
		$query = mysql_query("SELECT * FROM tbl_lookup_circle WHERE circle_id='$_REQUEST[id]' ");
		$detail = mysql_fetch_assoc($query);
		echo "<td height='27' width='100' ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$detail['zone_name']."</font></td>";
		echo "<td align='center' width='400' id='circle_id_". $detail['circle_id'] ."'><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$detail['circle_name']."</font></td>";
		echo "<td align='center'><a href='Javascript:void(0);' onClick=\" edit_zone('".$detail['circle_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Edit</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		echo "<span id='state_id_".$detail['circle_id']."'>";
				if($detail['status'] == "1") echo "<a href='Javascript:void(0);' onClick=\"status_handler(0,'".$detail['circle_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Disable</font></a>";
				else echo "<a href='Javascript:void(0);' onClick=\"status_handler(1,'".$detail['circle_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Enable</font></a>";
		
		echo "</span></td>";
		echo "<td width='100' ></td>";
		unset($query);
		unset($detail);
		
		
	}
	
	if(isset($_REQUEST['search'])){
	
	$query = mysql_query("SELECT DISTINCT c.circle_id FROM tbl_lookup_circle c , tbl_lookup_zone z WHERE c.circle_name like '$_REQUEST[search]%' OR c.zone_name like '$_REQUEST[search]%' AND c.zone_id = z.zone_id AND z.status = 1  ");
		$sub_id = array();
		while($data = mysql_fetch_assoc($query)) {
			$sub_id[] = $data;
		}
		
		echo json_encode($sub_id);
		unset($query);
		unset($sub_id);
	}

	
?>